package com.yanqu.road.server.cmd.marriage.cross;

import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.CrossMarriageGroupMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_UNION_MARRIAGE_APPLY_AGREE, desc = "同意跨服联姻请求")
public class CrossUnionMarriageApplyAgreeCmd extends AbstractCommand {

    private String myChildUid;
    private String partnerChildUid;
    private long partnerServerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossMarriageProto.CrossMarriageApplyAgreeReqMsg reqMsg = CrossMarriageProto.CrossMarriageApplyAgreeReqMsg.parseFrom(packet.getBytes());
        myChildUid = reqMsg.getMyChildUid();
        partnerChildUid = reqMsg.getPartnerChildUid();
        partnerServerId = reqMsg.getPartnerServerId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(partnerServerId > 0) {
            if (!ServerListMgr.isServerProvideServices(partnerServerId)) {
                ret = GameErrorCode.E_CROSS_MARRIAGE_PARTNER_SERVER_MAINTAIN;
                return;
            }
        }
        ret = MarriageMgr.agreeCrossUnionMarriage(player, myChildUid, partnerChildUid);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(0 != ret) {
            CrossMarriageProto.CrossMarriageApplyAgreeRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageApplyAgreeRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CROSS_UNION_MARRIAGE_APPLY_AGREE, respMsg);
        }
    }
}
